package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.NewPartyActivity;
import com.da.lon.wang.xlg.activity.PartyInfoActivity;
import com.da.lon.wang.xlg.activity.ProductsActivity;
import com.da.lon.wang.xlg.activity.WebViewActivity;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultGroupList;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList.PartyResultProList;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.view.HorizontalListView;

import java.util.ArrayList;
import java.util.List;

public class NewPartyLvAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
    Holder7 holder;
    private LayoutInflater inflater;
    private Context context;
    private ArrayList<PartyResultList> datas;
    private ArrayList<PartyResultGroupList> groupDatas;
    private String id = null;
    private List<String> productNumber;

    public NewPartyLvAdapter(Context context, ArrayList<PartyResultList> datas,
                             ArrayList<PartyResultGroupList> groupDatas) {
        this.context = context;
        this.datas = datas;
        this.groupDatas = groupDatas;
        productNumber = new ArrayList<String>();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (null != groupDatas && groupDatas.size() > 0 && null != datas) {
            return groupDatas.size() + datas.size();
        } else if (null != datas) {
            return datas.size();
        } else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        int INDEX = 0;
        if (null != groupDatas && groupDatas.size() > 0
                && position < groupDatas.size()) {
            INDEX = TYPE_ITEM;
        } else {
            INDEX = TYPE_SEPARATOR;
        }
        return INDEX;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        if (groupDatas == null) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 活动页面列表项
        try {
            // LogUtils.v("NewPartyLvAdapter_position:"+position);
            int type = getItemViewType(position);
            if (convertView == null) {
                holder = new Holder7();
                switch (type) {
                    case TYPE_ITEM:
                        convertView = inflater.inflate(
                                R.layout.ocoffee_newparty_head_list_item, null);
                        holder.list_head_item_imgv = (ImageView) convertView
                                .findViewById(R.id.list_head_item_imgv);
                        break;
                    case TYPE_SEPARATOR:
                        convertView = inflater.inflate(
                                R.layout.ocoffee_newparty_list_item, null);
                        holder.imageView = (ImageView) convertView
                                .findViewById(R.id.list_item_imgv);
                        holder.list_horizontal_sv = (HorizontalListView) convertView
                                .findViewById(R.id.list_horizontal_sv);

                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (Holder7) convertView.getTag();
            }

            switch (type) {
                case TYPE_ITEM:
                    MyApplication.loadImageByPicasso(context,
                            groupDatas.get(position).getImg_url(), null,
                            holder.list_head_item_imgv, 0, 0, 0);
                    holder.list_head_item_imgv
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    if (!MyApplication.isNetworkAvailable(context)) {
                                        return;
                                    }
                                    if (groupDatas.size() > position) {
                                        if (groupDatas.get(position).getStatus()
                                                .equals("1")) {
                                            id = groupDatas.get(position)
                                                    .getActivity_group_id();
                                            Intent intent = new Intent(context,
                                                    NewPartyActivity.class);
                                            Bundle bundle = new Bundle();
                                            bundle.putString("id", id);
                                            intent.putExtras(bundle);
                                            context.startActivity(intent);
                                        } else if (groupDatas.get(position)
                                                .getStatus().equals("2")) {
                                            Intent intent = new Intent(context,
                                                    WebViewActivity.class);
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("flag", 002);
                                            bundle.putString("url",
                                                    groupDatas.get(position)
                                                            .getUrl());
                                            intent.putExtras(bundle);
                                            context.startActivity(intent);
                                        } else {
                                            Intent intent = new Intent(context,
                                                    WebViewActivity.class);
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("flag", 002);
                                            bundle.putString("url",
                                                    groupDatas.get(position)
                                                            .getUrl());
                                            intent.putExtras(bundle);
                                            context.startActivity(intent);
                                        }
                                    }
                                }
                            });
                    break;

                case TYPE_SEPARATOR:

                    final int myposition = groupDatas == null ? position : position
                            - groupDatas.size();
                    PartyResultList partyResultList = datas.get(myposition);
                    final ArrayList<PartyResultProList> proDatas = datas.get(
                            myposition).getProducts_list();

                    MyApplication.loadImageByPicasso(context,
                            partyResultList.getImg_url(), null, holder.imageView,
                            0, 0, 0);
                    holder.imageView.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (!MyApplication.isNetworkAvailable(context)) {
                                return;
                            }
                            id = datas.get(myposition).getActivity_id();
                            Intent intent = new Intent(context,
                                    PartyInfoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("id", id);
                            intent.putExtras(bundle);
                            context.startActivity(intent);
                        }
                    });

                    NewHeadLvAdapter newHeadLvAdapter = new NewHeadLvAdapter(
                            context, proDatas);
                    holder.list_horizontal_sv.setAdapter(newHeadLvAdapter);
                    holder.list_horizontal_sv
                            .setOnItemClickListener(new OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                                        View arg1, int arg2, long arg3) {
                                    if (!MyApplication.isNetworkAvailable(context)) {
                                        return;
                                    }
                                    Intent intent = new Intent(context,
                                            ProductsActivity.class);
                                    String product_number = proDatas.get(arg2)
                                            .getProduct_number();
                                    PreferenceUtils.getInstance().SetSettingString(
                                            PreferencesConfig.PRODUCT_NUMBER,
                                            product_number);
                                    context.startActivity(intent);
                                }
                            });
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

}

class Holder7 {
    public HorizontalListView list_horizontal_sv;
    public ImageView imageView;
    ImageView list_head_item_imgv;
    LinearLayout scrollView;
    ImageView svImageView;
    TextView svNameTextView, svPriceTextView, svPriceSpeTextView;
}
